package com.aircity.commonuse.egg.service;

import com.aircity.commonuse.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Set;

/**
 * desc : 奖品事务
 * author : gongfangchao
 * date : 2020/6/11
 * Copyright (c) 2015 jigoon
 **/
@Service
public class PrizeService {

    public static Logger logger = LoggerFactory.getLogger(PrizeService.class);

    @Autowired
    private RedisUtil redisUtil;

    public void initLotteries(String lotteryKey){
        this.redisUtil.sAdd(lotteryKey, "1:1");
        this.redisUtil.sAdd(lotteryKey, "2:1","2:2");
        this.redisUtil.sAdd(lotteryKey, "3:1","3:2","3:3");
        for (int i=0; i<16; i++) {
            String value = "4:" + ( i + 1);
            this.redisUtil.sAdd(lotteryKey, value);
        }
    }

    public void drawLottery(Long userId, String lotteryKey) {
        Set<Object> lotteries = this.redisUtil.sGet(lotteryKey);
        if (lotteries.isEmpty()) {
            //todo 重新初始化奖品集合
            this.initLotteries(lotteryKey);
        }

        Object result = this.redisUtil.sPop(lotteryKey);
        logger.info("user {} get prize is {}", userId, result);
    }

}
